/**
 * Author: Balaram Putta
 * Date: Dec 16, 2003
 * Time: 3:16:04 PM
 */
package com.scalar.chiptrack.tags;

import java.util.HashMap;
import java.util.StringTokenizer;

import javax.servlet.jsp.tagext.TagSupport;

import com.scalar.chiptrack.utils.ConfigManager;

/**
 * Renders
 */
public class SiteMapTag extends TagSupport
{
    private static String JSP_PACKAGE = "org.apache.jsp.";
    private static String UNDERSCORE_SEPERATOR = "0005f";

    private static String chipTracContext = ConfigManager.getChipTracContext();
    private static HashMap pageSiteMapDetails = new HashMap();  // Cache the results

    /**
     * class property
     */
    private String m_styleClass = "";

    public String getStyleClass()
    {
        return m_styleClass;
    }

    public void setStyleClass( String styleClass )
    {
        if( styleClass != null )
        {
            m_styleClass = styleClass;
        }
    }

    public int doStartTag()
    {
        StringBuffer siteMapTextBuffer = new StringBuffer();
        String siteMapText = "";
        String jspClassName = null;

        try
        {
            jspClassName = pageContext.getPage().getClass().getName();
            HashMap stepNameValueMap = SiteMapConfig.getStepsByPage( getJspName( jspClassName ) );

            siteMapText = ( String ) pageSiteMapDetails.get( jspClassName );
            if( siteMapText == null )
            {
                if( ( stepNameValueMap != null ) && ( stepNameValueMap.isEmpty() == false ) )
                {
                    Object[] names = stepNameValueMap.keySet().toArray();
                    Object[] links = stepNameValueMap.values().toArray();
                    int size = names.length;

                    siteMapTextBuffer.append( "<table border='0' bordercolor='#FFFFFF' class='" + m_styleClass + "'><tr><td>" );
                    for( int i=0; i<size; i++ )
                    {
                        if( links[i] != null )
                        {
                            siteMapTextBuffer.append( "<a href=\"/" + chipTracContext + links[i] + "\">" + names[i] + "</a> &gt;&gt; " );
                        }
                        else
                        {
                            siteMapTextBuffer.append( names[i] );
                        }
                    }

                    siteMapTextBuffer.append( "</td></tr></table>" );
                    siteMapText = siteMapTextBuffer.toString();
                }
                else
                {
                    siteMapText = "";
                }

                pageSiteMapDetails.put( jspClassName, siteMapText );
            }
        }
        catch( Throwable t )
        {
            siteMapText = "";
            pageSiteMapDetails.put( jspClassName, siteMapText );

            System.out.println( "****** WARNING!! Could not render Site map tag " + t.getMessage() );
        }

        try
        {
            pageContext.getOut().print( siteMapText );
        }
        catch( Throwable t )
        {
            System.out.println( "****** WARNING!! Could not render Site map tag " + t.getMessage() );
        }

        return EVAL_BODY_INCLUDE;
    }


    /**
     * Returns JSP file name based on a given JSP Class name.
     * @param jspClassName
     * @return JSP file name
     */
    private String getJspName( String jspClassName )
    {
        jspClassName = jspClassName.substring( JSP_PACKAGE.length() );
        StringTokenizer st = new StringTokenizer( jspClassName, "_$" );

        StringBuffer jspPageNameBuffer = new StringBuffer();
        String token = null;

        while( st.hasMoreTokens() )
        {
            token = st.nextToken();
            if( token.equalsIgnoreCase( "jsp" ) != true )
            {
                if( token.startsWith( UNDERSCORE_SEPERATOR ) )
                {
                    token = "_" + token.substring( UNDERSCORE_SEPERATOR.length() );
                }
                jspPageNameBuffer.append( token );
            }
            else
            {
                jspPageNameBuffer.append( ".jsp" );
            }
        }

        //System.out.println( "jsp name = " + jspPageNameBuffer.toString() );

        return jspPageNameBuffer.toString();
    }
}
